package cn.edu.bupt.ch4_2.controller;

import cn.edu.bupt.ch4_2.common.Result;
import cn.edu.bupt.ch4_2.dto.ProductWithComments;
import cn.edu.bupt.ch4_2.entity.Comment;
import cn.edu.bupt.ch4_2.entity.Product;
import cn.edu.bupt.ch4_2.mapper.CommentMapper;
import cn.edu.bupt.ch4_2.mapper.ProductMapper;

import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;


@Tag(name = "商品及评论接口")
@RestController
@RequestMapping("/api/v1/product")
public class ProductController {

    @Resource
    ProductMapper productMapper;
    @Resource
    CommentMapper commentMapper;

    @Operation(summary = "获取商品列表", description = "返回值为全部的商品列表")
    @GetMapping(path = "/", produces = "application/json")
    public Result<?> listAll() {
        List<Product> products = productMapper.findAll();
        return Result.ok(products);
    }

    @Operation(summary = "获取所有商品及所有评论列表", description = "嵌套查询")
    @GetMapping(path = "/all/comment/all", produces = "application/json")
    public Result<?> listAllWithComments() {
        List<ProductWithComments> productWithComments = productMapper.findAllWithComments();
        return Result.ok(productWithComments);
    }

    @Operation(summary = "获取所有商品及所有评论列表V2", description = "映射嵌套结果集合")
    @GetMapping(path = "/all/comment/all/v2", produces = "application/json")
    public Result<?> listAllWithCommentsV2() {
        List<ProductWithComments> productWithComments = productMapper.findAllWithCommentsV2();
        return Result.ok(productWithComments);
    }

    @Operation(summary = "添加新的商品", description = "创建成功，返回状态码201")
    @PostMapping(path = "/", consumes = "application/json", produces = "application/json")
    public Result<?>  add(@RequestBody Product product) {
        productMapper.create(product);
        return Result.ok(product);
    }

    @Operation(summary = "根据id删除商品", description = "如果不存在，则返回状态码404，如果存在且删除成功，则返回状态码204")
    @DeleteMapping(path = "/{prodId}")
    public Result<?> delete(@PathVariable Long prodId) {
        int count = productMapper.deleteById(prodId);
        if (count>0) {
            return Result.ok();
        } else {
            return Result.error(204, "该商品不存在");
        }
    }

    @Operation(summary = "获取商品评论列表", description = "返回值为商品的评论列表")
    @GetMapping(path = "/{prodId}/comment/", produces = "application/json")
    public Result<?> commentListAll(@PathVariable int prodId) {
        List<Comment> comments = commentMapper.findByProdId(prodId);
        if (comments!= null) {
            return Result.ok(comments);
        } else {
            return Result.error(204, "评论不存在");
        }
    }

    @Operation(summary = "为商品添加评论", description = "创建成功，返回状态码201")
    @PostMapping(path = "/{prodId}/comment/", consumes = "application/json", produces = "application/json")
    public Result<?>  commentAdd(@PathVariable Long prodId, @RequestBody Comment comment) {
        Product product = productMapper.findById(prodId);
        if (product != null) {
            comment.setProductId(prodId);
            commentMapper.create(comment);
            return Result.ok(comment);
        } else {
            return Result.error(204, "该商品不存在");
        }
    }
}
